{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 知识点总结（期中更新）\n",
    "# 课程目标\n",
    "> * 应用编程及程序开发通用技能。\n",
    "> * 运用Python 语言来实践网站与移动应用开发\n",
    "> * 找寻、评估并使用Python 语言编程库及工具\n",
    "> * 加入并参与Python 语言项目开发。\n",
    "# 课程内容\n",
    "## python语言\n",
    "### 简介：\n",
    "> * Python是一种跨平台的计算机程序设计语言。 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell)，随着版本的不断更新和语言新功能的添加，越多被用于独立的、大型项目的开发。\n",
    "### 为什么选择python？\n",
    "> * 跟C/C++/Java相比 — Python能让你用少的多的多的代码写出相同的程序。有人计算过，Python或Ruby写出的程序的代码行数只相当于相对应的Java代码的行数的五分之一。\n",
    "> * 跟VB/PHP比较 — 跟PHP/VB相比，Python的是一种从设计上讲比它们好的不知多少倍的语言，可以用于开发大型项目。\n",
    ">> * 用python打印“Hello world”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### python的优点：\n",
    "> * 1.简单易懂：Python程序看上去总是简单易懂，初学者学Python，不但入门容易，而且将来深入下去，可以编写那些非常非常复杂的程序。\n",
    "> * 2.开发效率高：Python有非常强大的第三方库，基本上你想通过计算机实现任何功能，Python官方库里都有相应的模块进行支持，直接下载调用后。\n",
    "> * 3.高级语言：当你用Python语言编写程序的时候，你无需考虑诸如如何管理你的程序使用的内存一类的底层细节\n",
    "> * 4.可移植性：如果你小心地避免使用依赖于系统的特性，那么你的所有Python程序无需修改就几乎可以在市场上所有的系统平台上运行\n",
    "> * 5.可扩展性：如果你需要你的一段关键代码运行得更快或者希望某些算法不公开，你可以把你的部分程序用C或C++编写，然后在你的Python程序中使用它们。\n",
    "> * 6.可嵌入性：你可以把Python嵌入你的C/C++程序，从而向你的程序用户提供脚本功能。\n",
    "#### python的缺点：\n",
    "> * 1.速度慢，Python 的运行速度相比C语言确实慢很多，跟JAVA相比也要慢一些，但其实这里所指的运行速度慢在大多数情况下用户是无法直接感知到的。其实在大多数情况下Python已经完全可以满足你对程序速度的要求。\n",
    "> * 2.代码不能加密，因为Python是解释性语言，它的源码都是以名文形式存放的，不过我不认为这算是一个缺点，如果你的项目要求源代码必须是加密的，那你一开始就不应该用Python来去实现。\n",
    "> * 3.线程不能利用多CPU问题，这是Python被人诟病最多的一个缺点，GIL即全局解释器锁（Global Interpreter Lock），是计算机程序设计语言解释器用于同步线程的工具，使得任何时刻仅有一个线程在执行，Python的线程是操作系统的原生线程。在Linux上为pthread，在Windows上为Win thread，完全由操作系统调度线程的执行。一个python解释器进程内有一条主线程，以及多条用户程序的执行线程。即使在多核CPU平台上，由于GIL的存在，所以禁止多线程的并行执行。\n",
    "\n",
    "## Python 标准库\n",
    "> * 函数+模块=标准库\n",
    "### 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'win32'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#sys模块\n",
    "import sys\n",
    "sys.platform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\86135'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#os模块\n",
    "import os\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2020, 12, 1)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#datetime模块\n",
    "import datetime\n",
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'17:09'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#time模块\n",
    "import time\n",
    "time.strftime(\"%H:%M\")\n",
    "#时间模块：time与datetime\n",
    "#在Python中，通常有这几种方式来表示时间：\n",
    "#时间戳 指格林威治时间1970年01月01日00时00分00秒（北京时间1970年01月01日08时00分00秒）到现在等秒数（UNIX诞生元年）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数：变量、赋值、数据\n",
    "#### 变量\n",
    "> * 变量用于存储要在计算机程序中引用和操作的信息。它们还提供了一种用描述性名称标记数据的方法，以便读者和我们自己能够更清楚地理解我们的程序。将变量看作包含信息的容器是有帮助的。它们的唯一目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。\n",
    "> * 在python中直接用“=”进行赋值\n",
    ">> * name = bytes(y, encoding=‘utf-8’)\n",
    ">> * name = “y”\n",
    ">> * A = int (2);\n",
    ">> * B = float (2);\n",
    "> * 养成好习惯在进行python编程的时候在首行键入#_*_coding:utf-8_*_\n",
    "\n",
    "> * if语句:可以进行嵌套。不要超过三层，最多五层。\n",
    ">> * if 条件:\n",
    "      if-语句块\n",
    ">>> * if 条件:\n",
    ">>>> * if-语句块\n",
    ">>> * else 条件:\n",
    ">>>> * if-语句块\n",
    ">> * elif 条件:\n",
    ">>> * if-语句块\n",
    ">> * else 条件:\n",
    ">>> * else-语句块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "猜猜游戏\n",
      "请输入一个数字10\n",
      "我是小可爱\n",
      "游戏结束\n"
     ]
    }
   ],
   "source": [
    "# 利用if语句做一个简单的猜猜游戏项目\n",
    "print(\"猜猜游戏\")\n",
    "temp = input(\"请输入一个数字\")\n",
    "a = int(temp)\n",
    "if a == 8:/\n",
    "    print(\"我是帅哥美女\")\n",
    "elif a == 10:\n",
    "    print(\"我是小可爱\")\n",
    "else:\n",
    "    print(\"我是码农\")\n",
    "print(\"游戏结束\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### For循环和range内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#循环迭代数字变量\n",
    "for i in [1,2,3]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y\n",
      "o\n",
      "u\n",
      "g\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "#循环迭代字符串变量\n",
    "for ch in \"youge\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "#迭代指定的次数\n",
    "for num in range(5):\n",
    "    print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "6\n",
      "10\n",
      "15\n",
      "21\n",
      "28\n",
      "36\n",
      "45\n",
      "55\n",
      "66\n",
      "78\n",
      "91\n",
      "105\n",
      "120\n",
      "136\n",
      "153\n",
      "171\n",
      "190\n",
      "210\n",
      "231\n",
      "253\n",
      "276\n",
      "300\n",
      "325\n",
      "351\n",
      "378\n",
      "406\n",
      "435\n",
      "465\n",
      "496\n",
      "528\n",
      "561\n",
      "595\n",
      "630\n",
      "666\n",
      "703\n",
      "741\n",
      "780\n",
      "820\n",
      "861\n",
      "903\n",
      "946\n",
      "990\n",
      "1035\n",
      "1081\n",
      "1128\n",
      "1176\n",
      "1225\n",
      "1275\n",
      "1326\n",
      "1378\n",
      "1431\n",
      "1485\n",
      "1540\n",
      "1596\n",
      "1653\n",
      "1711\n",
      "1770\n",
      "1830\n",
      "1891\n",
      "1953\n",
      "2016\n",
      "2080\n",
      "2145\n",
      "2211\n",
      "2278\n",
      "2346\n",
      "2415\n",
      "2485\n",
      "2556\n",
      "2628\n",
      "2701\n",
      "2775\n",
      "2850\n",
      "2926\n",
      "3003\n",
      "3081\n",
      "3160\n",
      "3240\n",
      "3321\n",
      "3403\n",
      "3486\n",
      "3570\n",
      "3655\n",
      "3741\n",
      "3828\n",
      "3916\n",
      "4005\n",
      "4095\n",
      "4186\n",
      "4278\n",
      "4371\n",
      "4465\n",
      "4560\n",
      "4656\n",
      "4753\n",
      "4851\n",
      "4950\n",
      "5050\n",
      "5151\n",
      "5253\n",
      "5356\n",
      "5460\n",
      "5565\n",
      "5671\n",
      "5778\n",
      "5886\n",
      "5995\n",
      "6105\n",
      "6216\n",
      "6328\n",
      "6441\n",
      "6555\n",
      "6670\n",
      "6786\n",
      "6903\n",
      "7021\n",
      "7140\n",
      "7260\n",
      "7381\n",
      "7503\n",
      "7626\n",
      "7750\n",
      "7875\n",
      "8001\n",
      "8128\n",
      "8256\n",
      "8385\n",
      "8515\n",
      "8646\n",
      "8778\n",
      "8911\n",
      "9045\n",
      "9180\n",
      "9316\n",
      "9453\n",
      "9591\n",
      "9730\n",
      "9870\n",
      "10011\n",
      "10153\n",
      "10296\n",
      "10440\n",
      "10585\n",
      "10731\n",
      "10878\n",
      "11026\n",
      "11175\n",
      "11325\n",
      "11476\n",
      "11628\n",
      "11781\n",
      "11935\n",
      "12090\n",
      "12246\n",
      "12403\n",
      "12561\n",
      "12720\n",
      "12880\n",
      "13041\n",
      "13203\n",
      "13366\n",
      "13530\n",
      "13695\n",
      "13861\n",
      "14028\n",
      "14196\n",
      "14365\n",
      "14535\n",
      "14706\n",
      "14878\n",
      "15051\n",
      "15225\n",
      "15400\n",
      "15576\n",
      "15753\n",
      "15931\n",
      "16110\n",
      "16290\n",
      "16471\n",
      "16653\n",
      "16836\n",
      "17020\n",
      "17205\n",
      "17391\n",
      "17578\n",
      "17766\n",
      "17955\n",
      "18145\n",
      "18336\n",
      "18528\n",
      "18721\n",
      "18915\n",
      "19110\n",
      "19306\n",
      "19503\n",
      "19701\n",
      "19900\n",
      "20100\n",
      "20301\n",
      "20503\n",
      "20706\n",
      "20910\n",
      "21115\n",
      "21321\n",
      "21528\n",
      "21736\n",
      "21945\n",
      "22155\n",
      "22366\n",
      "22578\n",
      "22791\n",
      "23005\n",
      "23220\n",
      "23436\n",
      "23653\n",
      "23871\n",
      "24090\n",
      "24310\n",
      "24531\n",
      "24753\n",
      "24976\n",
      "25200\n",
      "25425\n",
      "25651\n",
      "25878\n",
      "26106\n",
      "26335\n",
      "26565\n",
      "26796\n",
      "27028\n",
      "27261\n",
      "27495\n",
      "27730\n",
      "27966\n",
      "28203\n",
      "28441\n",
      "28680\n",
      "28920\n",
      "29161\n",
      "29403\n",
      "29646\n",
      "29890\n",
      "30135\n",
      "30381\n",
      "30628\n",
      "30876\n",
      "31125\n",
      "31375\n",
      "31626\n",
      "31878\n",
      "32131\n",
      "32385\n",
      "32640\n",
      "32896\n",
      "33153\n",
      "33411\n",
      "33670\n",
      "33930\n",
      "34191\n",
      "34453\n",
      "34716\n",
      "34980\n",
      "35245\n",
      "35511\n",
      "35778\n",
      "36046\n",
      "36315\n",
      "36585\n",
      "36856\n",
      "37128\n",
      "37401\n",
      "37675\n",
      "37950\n",
      "38226\n",
      "38503\n",
      "38781\n",
      "39060\n",
      "39340\n",
      "39621\n",
      "39903\n",
      "40186\n",
      "40470\n",
      "40755\n",
      "41041\n",
      "41328\n",
      "41616\n",
      "41905\n",
      "42195\n",
      "42486\n",
      "42778\n",
      "43071\n",
      "43365\n",
      "43660\n",
      "43956\n",
      "44253\n",
      "44551\n",
      "44850\n",
      "45150\n",
      "45451\n",
      "45753\n",
      "46056\n",
      "46360\n",
      "46665\n",
      "46971\n",
      "47278\n",
      "47586\n",
      "47895\n",
      "48205\n",
      "48516\n",
      "48828\n",
      "49141\n",
      "49455\n",
      "49770\n",
      "50086\n",
      "50403\n",
      "50721\n",
      "51040\n",
      "51360\n",
      "51681\n",
      "52003\n",
      "52326\n",
      "52650\n",
      "52975\n",
      "53301\n",
      "53628\n",
      "53956\n",
      "54285\n",
      "54615\n",
      "54946\n",
      "55278\n",
      "55611\n",
      "55945\n",
      "56280\n",
      "56616\n",
      "56953\n",
      "57291\n",
      "57630\n",
      "57970\n",
      "58311\n",
      "58653\n",
      "58996\n",
      "59340\n",
      "59685\n",
      "60031\n",
      "60378\n",
      "60726\n",
      "61075\n",
      "61425\n",
      "61776\n",
      "62128\n",
      "62481\n",
      "62835\n",
      "63190\n",
      "63546\n",
      "63903\n",
      "64261\n",
      "64620\n",
      "64980\n",
      "65341\n",
      "65703\n",
      "66066\n",
      "66430\n",
      "66795\n",
      "67161\n",
      "67528\n",
      "67896\n",
      "68265\n",
      "68635\n",
      "69006\n",
      "69378\n",
      "69751\n",
      "70125\n",
      "70500\n",
      "70876\n",
      "71253\n",
      "71631\n",
      "72010\n",
      "72390\n",
      "72771\n",
      "73153\n",
      "73536\n",
      "73920\n",
      "74305\n",
      "74691\n",
      "75078\n",
      "75466\n",
      "75855\n",
      "76245\n",
      "76636\n",
      "77028\n",
      "77421\n",
      "77815\n",
      "78210\n",
      "78606\n",
      "79003\n",
      "79401\n",
      "79800\n",
      "80200\n",
      "80601\n",
      "81003\n",
      "81406\n",
      "81810\n",
      "82215\n",
      "82621\n",
      "83028\n",
      "83436\n",
      "83845\n",
      "84255\n",
      "84666\n",
      "85078\n",
      "85491\n",
      "85905\n",
      "86320\n",
      "86736\n",
      "87153\n",
      "87571\n",
      "87990\n",
      "88410\n",
      "88831\n",
      "89253\n",
      "89676\n",
      "90100\n",
      "90525\n",
      "90951\n",
      "91378\n",
      "91806\n",
      "92235\n",
      "92665\n",
      "93096\n",
      "93528\n",
      "93961\n",
      "94395\n",
      "94830\n",
      "95266\n",
      "95703\n",
      "96141\n",
      "96580\n",
      "97020\n",
      "97461\n",
      "97903\n",
      "98346\n",
      "98790\n",
      "99235\n",
      "99681\n",
      "100128\n",
      "100576\n",
      "101025\n",
      "101475\n",
      "101926\n",
      "102378\n",
      "102831\n",
      "103285\n",
      "103740\n",
      "104196\n",
      "104653\n",
      "105111\n",
      "105570\n",
      "106030\n",
      "106491\n",
      "106953\n",
      "107416\n",
      "107880\n",
      "108345\n",
      "108811\n",
      "109278\n",
      "109746\n",
      "110215\n",
      "110685\n",
      "111156\n",
      "111628\n",
      "112101\n",
      "112575\n",
      "113050\n",
      "113526\n",
      "114003\n",
      "114481\n",
      "114960\n",
      "115440\n",
      "115921\n",
      "116403\n",
      "116886\n",
      "117370\n",
      "117855\n",
      "118341\n",
      "118828\n",
      "119316\n",
      "119805\n",
      "120295\n",
      "120786\n",
      "121278\n",
      "121771\n",
      "122265\n",
      "122760\n",
      "123256\n",
      "123753\n",
      "124251\n",
      "124750\n",
      "125250\n",
      "125751\n",
      "126253\n",
      "126756\n",
      "127260\n",
      "127765\n",
      "128271\n",
      "128778\n",
      "129286\n",
      "129795\n",
      "130305\n",
      "130816\n",
      "131328\n",
      "131841\n",
      "132355\n",
      "132870\n",
      "133386\n",
      "133903\n",
      "134421\n",
      "134940\n",
      "135460\n",
      "135981\n",
      "136503\n",
      "137026\n",
      "137550\n",
      "138075\n",
      "138601\n",
      "139128\n",
      "139656\n",
      "140185\n",
      "140715\n",
      "141246\n",
      "141778\n",
      "142311\n",
      "142845\n",
      "143380\n",
      "143916\n",
      "144453\n",
      "144991\n",
      "145530\n",
      "146070\n",
      "146611\n",
      "147153\n",
      "147696\n",
      "148240\n",
      "148785\n",
      "149331\n",
      "149878\n",
      "150426\n",
      "150975\n",
      "151525\n",
      "152076\n",
      "152628\n",
      "153181\n",
      "153735\n",
      "154290\n",
      "154846\n",
      "155403\n",
      "155961\n",
      "156520\n",
      "157080\n",
      "157641\n",
      "158203\n",
      "158766\n",
      "159330\n",
      "159895\n",
      "160461\n",
      "161028\n",
      "161596\n",
      "162165\n",
      "162735\n",
      "163306\n",
      "163878\n",
      "164451\n",
      "165025\n",
      "165600\n",
      "166176\n",
      "166753\n",
      "167331\n",
      "167910\n",
      "168490\n",
      "169071\n",
      "169653\n",
      "170236\n",
      "170820\n",
      "171405\n",
      "171991\n",
      "172578\n",
      "173166\n",
      "173755\n",
      "174345\n",
      "174936\n",
      "175528\n",
      "176121\n",
      "176715\n",
      "177310\n",
      "177906\n",
      "178503\n",
      "179101\n",
      "179700\n",
      "180300\n",
      "180901\n",
      "181503\n",
      "182106\n",
      "182710\n",
      "183315\n",
      "183921\n",
      "184528\n",
      "185136\n",
      "185745\n",
      "186355\n",
      "186966\n",
      "187578\n",
      "188191\n",
      "188805\n",
      "189420\n",
      "190036\n",
      "190653\n",
      "191271\n",
      "191890\n",
      "192510\n",
      "193131\n",
      "193753\n",
      "194376\n",
      "195000\n",
      "195625\n",
      "196251\n",
      "196878\n",
      "197506\n",
      "198135\n",
      "198765\n",
      "199396\n",
      "200028\n",
      "200661\n",
      "201295\n",
      "201930\n",
      "202566\n",
      "203203\n",
      "203841\n",
      "204480\n",
      "205120\n",
      "205761\n",
      "206403\n",
      "207046\n",
      "207690\n",
      "208335\n",
      "208981\n",
      "209628\n",
      "210276\n",
      "210925\n",
      "211575\n",
      "212226\n",
      "212878\n",
      "213531\n",
      "214185\n",
      "214840\n",
      "215496\n",
      "216153\n",
      "216811\n",
      "217470\n",
      "218130\n",
      "218791\n",
      "219453\n",
      "220116\n",
      "220780\n",
      "221445\n",
      "222111\n",
      "222778\n",
      "223446\n",
      "224115\n",
      "224785\n",
      "225456\n",
      "226128\n",
      "226801\n",
      "227475\n",
      "228150\n",
      "228826\n",
      "229503\n",
      "230181\n",
      "230860\n",
      "231540\n",
      "232221\n",
      "232903\n",
      "233586\n",
      "234270\n",
      "234955\n",
      "235641\n",
      "236328\n",
      "237016\n",
      "237705\n",
      "238395\n",
      "239086\n",
      "239778\n",
      "240471\n",
      "241165\n",
      "241860\n",
      "242556\n",
      "243253\n",
      "243951\n",
      "244650\n",
      "245350\n",
      "246051\n",
      "246753\n",
      "247456\n",
      "248160\n",
      "248865\n",
      "249571\n",
      "250278\n",
      "250986\n",
      "251695\n",
      "252405\n",
      "253116\n",
      "253828\n",
      "254541\n",
      "255255\n",
      "255970\n",
      "256686\n",
      "257403\n",
      "258121\n",
      "258840\n",
      "259560\n",
      "260281\n",
      "261003\n",
      "261726\n",
      "262450\n",
      "263175\n",
      "263901\n",
      "264628\n",
      "265356\n",
      "266085\n",
      "266815\n",
      "267546\n",
      "268278\n",
      "269011\n",
      "269745\n",
      "270480\n",
      "271216\n",
      "271953\n",
      "272691\n",
      "273430\n",
      "274170\n",
      "274911\n",
      "275653\n",
      "276396\n",
      "277140\n",
      "277885\n",
      "278631\n",
      "279378\n",
      "280126\n",
      "280875\n",
      "281625\n",
      "282376\n",
      "283128\n",
      "283881\n",
      "284635\n",
      "285390\n",
      "286146\n",
      "286903\n",
      "287661\n",
      "288420\n",
      "289180\n",
      "289941\n",
      "290703\n",
      "291466\n",
      "292230\n",
      "292995\n",
      "293761\n",
      "294528\n",
      "295296\n",
      "296065\n",
      "296835\n",
      "297606\n",
      "298378\n",
      "299151\n",
      "299925\n",
      "300700\n",
      "301476\n",
      "302253\n",
      "303031\n",
      "303810\n",
      "304590\n",
      "305371\n",
      "306153\n",
      "306936\n",
      "307720\n",
      "308505\n",
      "309291\n",
      "310078\n",
      "310866\n",
      "311655\n",
      "312445\n",
      "313236\n",
      "314028\n",
      "314821\n",
      "315615\n",
      "316410\n",
      "317206\n",
      "318003\n",
      "318801\n",
      "319600\n",
      "320400\n",
      "321201\n",
      "322003\n",
      "322806\n",
      "323610\n",
      "324415\n",
      "325221\n",
      "326028\n",
      "326836\n",
      "327645\n",
      "328455\n",
      "329266\n",
      "330078\n",
      "330891\n",
      "331705\n",
      "332520\n",
      "333336\n",
      "334153\n",
      "334971\n",
      "335790\n",
      "336610\n",
      "337431\n",
      "338253\n",
      "339076\n",
      "339900\n",
      "340725\n",
      "341551\n",
      "342378\n",
      "343206\n",
      "344035\n",
      "344865\n",
      "345696\n",
      "346528\n",
      "347361\n",
      "348195\n",
      "349030\n",
      "349866\n",
      "350703\n",
      "351541\n",
      "352380\n",
      "353220\n",
      "354061\n",
      "354903\n",
      "355746\n",
      "356590\n",
      "357435\n",
      "358281\n",
      "359128\n",
      "359976\n",
      "360825\n",
      "361675\n",
      "362526\n",
      "363378\n",
      "364231\n",
      "365085\n",
      "365940\n",
      "366796\n",
      "367653\n",
      "368511\n",
      "369370\n",
      "370230\n",
      "371091\n",
      "371953\n",
      "372816\n",
      "373680\n",
      "374545\n",
      "375411\n",
      "376278\n",
      "377146\n",
      "378015\n",
      "378885\n",
      "379756\n",
      "380628\n",
      "381501\n",
      "382375\n",
      "383250\n",
      "384126\n",
      "385003\n",
      "385881\n",
      "386760\n",
      "387640\n",
      "388521\n",
      "389403\n",
      "390286\n",
      "391170\n",
      "392055\n",
      "392941\n",
      "393828\n",
      "394716\n",
      "395605\n",
      "396495\n",
      "397386\n",
      "398278\n",
      "399171\n",
      "400065\n",
      "400960\n",
      "401856\n",
      "402753\n",
      "403651\n",
      "404550\n",
      "405450\n",
      "406351\n",
      "407253\n",
      "408156\n",
      "409060\n",
      "409965\n",
      "410871\n",
      "411778\n",
      "412686\n",
      "413595\n",
      "414505\n",
      "415416\n",
      "416328\n",
      "417241\n",
      "418155\n",
      "419070\n",
      "419986\n",
      "420903\n",
      "421821\n",
      "422740\n",
      "423660\n",
      "424581\n",
      "425503\n",
      "426426\n",
      "427350\n",
      "428275\n",
      "429201\n",
      "430128\n",
      "431056\n",
      "431985\n",
      "432915\n",
      "433846\n",
      "434778\n",
      "435711\n",
      "436645\n",
      "437580\n",
      "438516\n",
      "439453\n",
      "440391\n",
      "441330\n",
      "442270\n",
      "443211\n",
      "444153\n",
      "445096\n",
      "446040\n",
      "446985\n",
      "447931\n",
      "448878\n",
      "449826\n",
      "450775\n",
      "451725\n",
      "452676\n",
      "453628\n",
      "454581\n",
      "455535\n",
      "456490\n",
      "457446\n",
      "458403\n",
      "459361\n",
      "460320\n",
      "461280\n",
      "462241\n",
      "463203\n",
      "464166\n",
      "465130\n",
      "466095\n",
      "467061\n",
      "468028\n",
      "468996\n",
      "469965\n",
      "470935\n",
      "471906\n",
      "472878\n",
      "473851\n",
      "474825\n",
      "475800\n",
      "476776\n",
      "477753\n",
      "478731\n",
      "479710\n",
      "480690\n",
      "481671\n",
      "482653\n",
      "483636\n",
      "484620\n",
      "485605\n",
      "486591\n",
      "487578\n",
      "488566\n",
      "489555\n",
      "490545\n",
      "491536\n",
      "492528\n",
      "493521\n",
      "494515\n",
      "495510\n",
      "496506\n",
      "497503\n",
      "498501\n",
      "499500\n",
      "500500\n"
     ]
    }
   ],
   "source": [
    "#用for循环求解整数1+2+3+….+1000的的值\n",
    "sum = 0\n",
    "for i in range(1,1001):\n",
    "    sum=sum+i\n",
    "    print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Random()模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['BPF',\n",
       " 'LOG4',\n",
       " 'NV_MAGICCONST',\n",
       " 'RECIP_BPF',\n",
       " 'Random',\n",
       " 'SG_MAGICCONST',\n",
       " 'SystemRandom',\n",
       " 'TWOPI',\n",
       " '_Sequence',\n",
       " '_Set',\n",
       " '__all__',\n",
       " '__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " '_accumulate',\n",
       " '_acos',\n",
       " '_bisect',\n",
       " '_ceil',\n",
       " '_cos',\n",
       " '_e',\n",
       " '_exp',\n",
       " '_inst',\n",
       " '_log',\n",
       " '_os',\n",
       " '_pi',\n",
       " '_random',\n",
       " '_repeat',\n",
       " '_sha512',\n",
       " '_sin',\n",
       " '_sqrt',\n",
       " '_test',\n",
       " '_test_generator',\n",
       " '_urandom',\n",
       " '_warn',\n",
       " 'betavariate',\n",
       " 'choice',\n",
       " 'choices',\n",
       " 'expovariate',\n",
       " 'gammavariate',\n",
       " 'gauss',\n",
       " 'getrandbits',\n",
       " 'getstate',\n",
       " 'lognormvariate',\n",
       " 'normalvariate',\n",
       " 'paretovariate',\n",
       " 'randint',\n",
       " 'random',\n",
       " 'randrange',\n",
       " 'sample',\n",
       " 'seed',\n",
       " 'setstate',\n",
       " 'shuffle',\n",
       " 'triangular',\n",
       " 'uniform',\n",
       " 'vonmisesvariate',\n",
       " 'weibullvariate']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "dir(random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "random.randint(1,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "#综合应用案例程序\n",
    "from datetime import datetime\n",
    "import random\n",
    "import time\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "right_this_minute = datetime.today().minute\n",
    "wait_time = random.randint(1,10)\n",
    "if right_this_minute in odds:\n",
    "    time.sleep(wait_time)\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    time.sleep(wait_time)\n",
    "    print(\"Not an odd minute\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 掌握评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-83f3a722f06e>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-83f3a722f06e>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    \"JUPYTE\":\"\",\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "python_base={\n",
    "       \"IDE\":{  \n",
    "               \"IDLE\":[\"操作，100（90）\",\"意义、100（90）\",\n",
    "               \"JUPYTE\":\"\",\n",
    "               \"pycharm\":\"\",\n",
    "                     }\n",
    "                       “XXX”:{}\n",
    "               }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处理结构化数据\n",
    "### 数字、字符与对象\n",
    "> * 在Python中一切都是对象。这意味着数字、字符串、函数、模块、所有的一切都是对象。其直接的结果是所有的对象都可以赋予变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字\n",
    "import random\n",
    "wait_time= random.randint(1,60)\n",
    "wait_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huijknhiohoas'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串\n",
    "word=\"huijknhiohoas\"\n",
    "word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABCDE'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数\n",
    "\"abcde\".upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huiox'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"HUIOX\".lower()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表\n",
    "> * Python中列表非常类似其他编程语言中数组的概念，因为你可以把列表想成是一个相关对象的索引集合，列表中的每个槽(元素)是从0开始编号的\n",
    "> * 与很多其他编程语言中的数组不同，python中的列表是动态的，因为他们可以根据需要扩展（和收缩）。使用列表存储任何对象之前不需要预声明列表的大小。\n",
    "> * 列表是可变的，因为可以在任何时间通过增加、删除或修改对象的类型，如果你愿意，完全可以在一个列表中混合不同类型的对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 'python']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python中如何创建列表\n",
    "a = [10,20,30,\"python\"]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 'python', 22]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# append()方法 在列表的末尾添加一个对象\n",
    "a.append(22)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 30, 'python', 22, 'g', 'i', 'g', 'c', 'b', 'h', 'j', 'b', 'i', 'd']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# extend()方法 在列表的添加一个可以迭代的对象，添加单个元素会报错\n",
    "a.extend(\"gigcbhjbid\")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# python中的insert()函数\n",
    "nums=[2,3,4]\n",
    "nums.insert(0,1)\n",
    "#insert(a,b)含义是在a处添加b\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python中pop()函数\n",
    "# pop() 函数用于移除列表中的一个元素（默认最后一个元素），并且返回该元素的值。\n",
    "a = [1,2,3,4,5]\n",
    "a.pop()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a.pop(0)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 7, 8, 2, 5, 43]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两个列表进行合并  用+\n",
    "a = [1,4,7,8]\n",
    "b = [2,5,43]\n",
    "c = a+b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 7, 8, 2, 4, 5, 43]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用+= 来合并列表\n",
    "A = [1,4,7,8]\n",
    "B = [2,4,5,43]\n",
    "A +=B\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用list()创建列表: 使用 list()可以将任何可迭代的数据转化成列表。 \n",
    "a = list()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(range(10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['j', 'a', 'c', 'k', ',', 'a', 'b', 'c', '1', '2', '3']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(\"jack,abc123\")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list([1,2,3,4,5])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 7, 9, 11, 13]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range()创建整数列表\n",
    "# range([start,] end [,step])\n",
    "# start 参数：可选，表示起始数字。默认是 0\n",
    "# end参数：必选，表示结尾数字。\n",
    "# step参数：可选，表示步长，默认为1\n",
    "# python3 中 range()返回的是一个range 对象，而不是列表。我们需要通过list()方法将其 转换成列表对象。\n",
    "list(range(3,15,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(15,3,-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 推导式创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 推导式生成列表(简介一下，重点在for循环后)\n",
    "a = [x*2 for x in range(5)]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = [x*2 for x in range(100) if x%9 ==0]\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表元素取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 10)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = range(0,10)\n",
    "b = list(a)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = b[0]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = b[9]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 6, 7, 2, 3]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一个小项目\n",
    "# 用random  模块中的  sample随机创建在区间1到10中包含5个元素的数组a\n",
    "import random\n",
    "a = random.sample(range(1,10),5)\n",
    "a\n",
    "#请随便输入一个1到10之间的一个数字，如果这个数字在上述数组a中，则在该数组的末尾加入该元素，如果该数字不在这个这个数组a中，就在第三个元素前面插入这个数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组\n",
    "> * 元组是一个一旦创建就不能改变的列表。任何情况下这个元组都不能再改变。\n",
    ">> 通常可以把元组想象成一个常量列表。\n",
    "> * 元组的使用场景：\n",
    ">> * 1.函数的参数和返回值，一个函数可以接收任意多个参数，一次返回多个数据；\n",
    ">> * 2.格式字符串；\n",
    ">> * 3.让列表不可以被修改，保护数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "youge is my teacher\n"
     ]
    }
   ],
   "source": [
    "print(\"%s is my %s\"%(\"youge\",\"teacher\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1, 3, 5, 6, 89, 212, 222)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (1,1,3,5,6,89,212,222)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1, 3, 5, 6, 89, 212, 222)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1,1,3,5,6,89,212,222\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元组中的方法\n",
    "* 元组只有两个方法，count和index。\n",
    "> * Python 元组 index() 方法用于从元组中找出某个对象第一个匹配项的索引位置，如果这个对象不在元组中会报一个异常。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 5, 2, 4, 5, 9, 2, 3, 5, 6, 7)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (1,5,2,4,5,9,2,3,5,6,7)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> * Python中的count() 方法用于统计某个元素在元组中出现的次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.count(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元组中的元素取值\n",
    "*   a = (3, 2, 1, 4, 5, 2, 2, 2, 4, 5, 6,  4)\n",
    "* 索引值 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (3, 2, 1, 4, 5, 2, 2, 2, 4, 5, 6,  4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[0]\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[3]\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### While循环\n",
    "* While  循环 ：  重点关注的是 需要加入循环结束条件，然后break\n",
    "> * while 判断条件(condition)： 执行语句(statements)……"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是我心中最美 0\n",
      "你是我心中最美 1\n",
      "你是我心中最美 2\n",
      "你是我心中最美 3\n",
      "你是我心中最美 4\n",
      "你是我心中最美 5\n",
      "你是我心中最美 6\n",
      "你是我心中最美 7\n",
      "你是我心中最美 8\n",
      "你是我心中最美 9\n",
      "你是我心中最美 10\n",
      "你是我心中最美 11\n",
      "你是我心中最美 12\n",
      "你是我心中最美 13\n",
      "你是我心中最美 14\n",
      "你是我心中最美 15\n",
      "你是我心中最美 16\n",
      "你是我心中最美 17\n",
      "你是我心中最美 18\n",
      "你是我心中最美 19\n",
      "你是我心中最美 20\n",
      "你是我心中最美 21\n",
      "你是我心中最美 22\n",
      "你是我心中最美 23\n",
      "你是我心中最美 24\n",
      "你是我心中最美 25\n",
      "你是我心中最美 26\n",
      "你是我心中最美 27\n",
      "你是我心中最美 28\n",
      "你是我心中最美 29\n",
      "你是我心中最美 30\n",
      "你是我心中最美 31\n",
      "你是我心中最美 32\n",
      "你是我心中最美 33\n",
      "你是我心中最美 34\n",
      "你是我心中最美 35\n",
      "你是我心中最美 36\n",
      "你是我心中最美 37\n",
      "你是我心中最美 38\n",
      "你是我心中最美 39\n",
      "你是我心中最美 40\n",
      "你是我心中最美 41\n",
      "你是我心中最美 42\n",
      "你是我心中最美 43\n",
      "你是我心中最美 44\n",
      "你是我心中最美 45\n",
      "你是我心中最美 46\n",
      "你是我心中最美 47\n",
      "你是我心中最美 48\n",
      "你是我心中最美 49\n",
      "你是我心中最美 50\n",
      "你是我心中最美 51\n",
      "你是我心中最美 52\n",
      "你是我心中最美 53\n",
      "你是我心中最美 54\n",
      "你是我心中最美 55\n",
      "你是我心中最美 56\n",
      "你是我心中最美 57\n",
      "你是我心中最美 58\n",
      "你是我心中最美 59\n",
      "你是我心中最美 60\n",
      "你是我心中最美 61\n",
      "你是我心中最美 62\n",
      "你是我心中最美 63\n",
      "你是我心中最美 64\n",
      "你是我心中最美 65\n",
      "你是我心中最美 66\n",
      "你是我心中最美 67\n",
      "你是我心中最美 68\n",
      "你是我心中最美 69\n",
      "你是我心中最美 70\n",
      "你是我心中最美 71\n",
      "你是我心中最美 72\n",
      "你是我心中最美 73\n",
      "你是我心中最美 74\n",
      "你是我心中最美 75\n",
      "你是我心中最美 76\n",
      "你是我心中最美 77\n",
      "你是我心中最美 78\n",
      "你是我心中最美 79\n",
      "你是我心中最美 80\n",
      "你是我心中最美 81\n",
      "你是我心中最美 82\n",
      "你是我心中最美 83\n",
      "你是我心中最美 84\n",
      "你是我心中最美 85\n",
      "你是我心中最美 86\n",
      "你是我心中最美 87\n",
      "你是我心中最美 88\n",
      "你是我心中最美 89\n",
      "你是我心中最美 90\n",
      "你是我心中最美 91\n",
      "你是我心中最美 92\n",
      "你是我心中最美 93\n",
      "你是我心中最美 94\n",
      "你是我心中最美 95\n",
      "你是我心中最美 96\n",
      "你是我心中最美 97\n",
      "你是我心中最美 98\n",
      "你是我心中最美 99\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while True:\n",
    "    print(\"你是我心中最美\",count)\n",
    "    count +=1\n",
    "    if count ==100: # 加入循环结束的条件\n",
    "        break   #跳出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "a =0\n",
    "while a< 10:\n",
    "    print(a)\n",
    "    a +=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "a =0\n",
    "while a< 10:\n",
    "    print(a)\n",
    "    a = a + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你猜想的数字：23\n",
      "你猜的数字太小了\n",
      "请输入你猜想的数字：55\n",
      "你猜的数字太小了\n",
      "请输入你猜想的数字：80\n",
      "你猜的数字太大了\n",
      "请输入你猜想的数字：70\n",
      "你猜的数字太大了\n",
      "请输入你猜想的数字：66\n",
      "你猜的数字太大了\n",
      "猜了这么都不对\n",
      "正确答案：63\n"
     ]
    }
   ],
   "source": [
    "#猜字游戏\n",
    "#使用While 与列表写一个猜字的游戏\n",
    "import random\n",
    "hide_card = random.randint(1,100)\n",
    "count = 0\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess = input(\"请输入你猜想的数字：\")\n",
    "            guess_digit =int(guess)\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"恭喜你，你猜中了\")\n",
    "                break\n",
    "            elif guess_digit >hide_card:\n",
    "                print(\"你猜的数字太大了\")\n",
    "            else:\n",
    "                print(\"你猜的数字太小了\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜了这么都不对\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0~99之间的数字\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典\n",
    "* 字典，字典是一种key - value 的数据类型，又叫做键/值对\n",
    "> * 字典= {字段名1：字段值，字段名2：字段值，字段名3：字段值}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1901': 'ziqi', 'stu1902': 'xiaofeng', 'stu1903': 'liyu'}\n"
     ]
    }
   ],
   "source": [
    "info ={\n",
    "    \"stu1901\" : \"ziqi\",\n",
    "    \"stu1902\":\"xiaofeng\",\n",
    "    \"stu1903\":\"liyu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> * 特性，字典是无序的，但是key  是唯一的，不可变性的如果重复最后的一个键值对会替换前面的，值不需要唯一。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict = {\"a\":1,\"b\":2,\"b\":3}\n",
    "dict[\"b\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 3}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 字典，值可以取任何数据类型，但键必须是不可变的，如字符串，数字或元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 简单字典实例\n",
    "dict = {'Alice':'2341','Beth':'9102','Cecil':'3258'}\n",
    "#也可以这样创建字典\n",
    "dict1 = { 'abc': 456 }\n",
    "dict2 = { 'abc': 123, 98.6:37 }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典的操作\n",
    "1. 修改字典-增加\n",
    "> * 向字典添加新内容的方法是增加新的键/值对，修改或删除已有键/值对。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict = {'a':'1','b':'2','c':'3'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict['b'] = 4\n",
    "dict['d'] = \"5\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(dict['b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(dict['d'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 修改字典——删除\n",
    "> * 能删单一的元素也能清空字典，清空只需一项操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b': 4, 'c': '3', 'd': '5'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del dict['a']\n",
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dict' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-50-1de715364dfd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#全清空\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mdel\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'dict' is not defined"
     ]
    }
   ],
   "source": [
    "#全清空\n",
    "del dict\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> * 随机删除。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b': '2', 'c': '3'}\n"
     ]
    }
   ],
   "source": [
    "dict = {'a':'1','b':'2','c':'3'}\n",
    "red = dict.pop('a')  #删除字典中的指定键值对并返回值\n",
    "print(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(red) # 查看返回的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b': '2'} 随机删除了\n"
     ]
    }
   ],
   "source": [
    "dict.popitem() #随机删除\n",
    "print(dict,'随机删除了')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 字典的内置函数与方法\n",
    "> * Python字典的len()函数：  计算字典元素的个数，即键的总数\n",
    "> * Python字典的str()函数：输出字典可打印的字符串表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Python字典的len()函数：  计算字典元素的个数，即键的总数\n",
    "dict = {'a':'1','b':'2','c':'3'}\n",
    "len(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'a': '1', 'b': '2', 'c': '3'}\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Python字典的str()函数：输出字典可打印的字符串表示\n",
    "dict = {'a':'1','b':'2','c':'3'}\n",
    "str(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': '1', 'b': '2', 'c': '3'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(dict)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 字典的键入形式\n",
    "> * 字典的键入形式要尽量的简单易懂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#第一种\n",
    "dict = {\n",
    "    'a':'1',\n",
    "    'b':'2',\n",
    "    'c':'3'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#第二种\n",
    "dict = {'a':'1','b':'2','c':'3'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典的嵌套\n",
    "* 访问一个复杂的数据\n",
    "> * 字典嵌套就是字典中还有字典，就是多级字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "dict ={\n",
    "    '321':{\n",
    "    'a':'1',\n",
    "    'b':'2',\n",
    "    'c':'3'\n",
    "    },\n",
    "    '123':{\n",
    "    'd':'4',\n",
    "    'e':'5',\n",
    "    'f':'6'\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'321': {'a': '1', 'b': '2', 'c': '3'},\n",
       " '123': {'d': '4', 'e': '5', 'f': '6'},\n",
       " 'e': 30}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典中如何增加一个新的键值对\n",
    "dict['e']=30\n",
    "dict\n",
    "#给字典中已有的键值对赋予一个新值 ，就把原字典中的已有的键值对更新了\n",
    "#可以先创建一个空字典，不断的增加键值对来更新字典，得到我们想要的字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典的频度计数引用\n",
    "> * 频度计数器先将频数初始化为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}\n",
      "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}\n",
      "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}\n"
     ]
    }
   ],
   "source": [
    "found ={}\n",
    "found[\"a\"]= 0\n",
    "found[\"e\"]= 0\n",
    "found[\"i\"]= 0\n",
    "found[\"o\"]= 0\n",
    "found[\"u\"]= 0\n",
    "print(found)\n",
    "found[\"e\"] = found[\"e\"]+1\n",
    "print(found)\n",
    "found[\"e\"] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典中  in与not in的用法\n",
    "> * 可以用 in 操作符来检查一个字典中是否存在这个键，这样可以有效的避免字典取值时出现的keyerror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 10}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits[\"apple\"] = 10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"apple\" in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"123\" in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "if \"bananas\" in fruits:\n",
    "    fruits[\"bananas\"] +=1\n",
    "else:\n",
    "    fruits[\"bananas\"]=1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> * 一般程序员用not in 来判断键是否在字典中，若不在初始化一个0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple': 10, 'bananas': 1}\n"
     ]
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits[\"apple\"] = 10\n",
    "if \"bananas\" not in fruits:\n",
    "    fruits[\"bananas\"]=0\n",
    "    fruits[\"bananas\"] +=1\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合\n",
    "* 集合不允许有重复，集合是用大括号包围，集合中只有逗号，绝对没有冒号\n",
    "> * 创建集合时，相同的元素会自动合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"a\",\"e\",\"e\"}\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'h', 'i', 's', 'u'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合中用set() 函数可以快速创建一个集合（例如一个字符串），\n",
    "a = set(\"huibcias\")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'h', 'i', 's', 'u']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用sorted()与list()函数将一个集合转化为一个有序的列表\n",
    "a ={ 'b','h','a', 'i', 's', 'c',  'u'}\n",
    "b = sorted(list(a))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'e', 'h', 'i', 'l', 'o', 's', 'u'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#union合并集合\n",
    "a = {'a', 'b', 'c', 'h', 'i', 's', 'u'}\n",
    "word = \"hello\"\n",
    "u = a.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'i', 's', 'u'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#defference()告诉你哪些不是共有的元素\n",
    "a = {'a', 'b', 'c', 'h', 'i', 's', 'u'}\n",
    "word = \"hello\"\n",
    "b = a.difference(set(word))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# intersection()报告共同对象\n",
    "a = {'a', 'b', 'c', 'h', 'i', 's', 'u'}\n",
    "word = \"hello\"\n",
    "b = a.intersection(set(word))\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 其中difference函数将a与set(word)中的对象进行比较，然后返回一个新的对象集合，其中包含a集合但是不包含set(word)集合的对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数与模块\n",
    "### 函数的定义\n",
    "* 重用Python中的代码，以函数开始，也是以函数结束。 取几行代码，为它们指定一个名字 ，你就得到了一个（可以重用的）函数，取一组函数把他们打包成一个文件，你就得到了一个模块（也可以重用）。\n",
    "> * 1.函数引入两个关键字：def和return\n",
    "> * 2.函数可以接受参数数据def后面跟着有的（a,b）这样的。\n",
    "> * 3.函数包含代码，通常还有注释文档(一般带有#)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数引入两个关键字：def和return\n",
    "def text(n):\n",
    "    result =[]\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        result.append(a)\n",
    "        a,b = b,a+b\n",
    "    return result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 一旦确定希望重要的Python代码块，就可以创建一个函数。要用def关键字（这是define的缩写）创建函数。def关键字后面是函数名、一个可选的空参数表(用小括号包围)，一个冒号，然后一行或多行代码。\n",
    "\n",
    "添加注释：\n",
    "* 为代码增加注释有两种方法，可以使用三重引号字符串””” str“””，这个称为docstring ，也可以使用单行注释，这种注释有种 #  符号前缀\n",
    "\n",
    "* Python代码样式指南 [教授程序员规范的码代码](https://legacy.python.org/dev/peps/pep-0008/)\n",
    "\n",
    "### 函数的传递参数\n",
    "* Python函数中的形参与实参：\n",
    "> * greet_user(username) #username 为形参；函数完成其工作时所需要的信息。\n",
    "> * greet_user(“youge\") #“youge\"为实参；实参是调用函数时传递给函数的信息。\n",
    "\n",
    "区别：形参是虚拟的，不占用空间，形参变量只有在被调用时才分配内存单元，实参是一个变量，占用内存空间，数据传递单向，实参传给形参，不能倒过来。\n",
    "### 函数的返回值\n",
    "* 函数返回的值被称为函数的返回值，在函数中，可用return语句将值返回到调用函数的代码行；\n",
    "> * 有返回值可以直接进行赋值\n",
    "* 让实参变得可选:\n",
    "> * 有时候，需要让实参变成可选的，这样使用函数的人就只需要在必要时候才提供额外信息\n",
    "\n",
    "#### 函数返回多个值\n",
    "* 函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'l', 'l', 'o', ' ', 'y', 'o', 'u', 'g', 'e']\n"
     ]
    }
   ],
   "source": [
    "# 返回列表\n",
    "def search_list(self):\n",
    "    a = []\n",
    "    for i in self:\n",
    "        a.append(i)\n",
    "    return a\n",
    "list_test = search_list('hello youge')\n",
    "print(list_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'first': 'jimi', 'last': 'hendrix'}\n"
     ]
    }
   ],
   "source": [
    "# 返回字典\n",
    "def build_person(first_name,last_name):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'first':first_name,'last':last_name}\n",
    "    return person\n",
    "\n",
    "musician = build_person('jimi','hendrix')\n",
    "print(musician)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'o', 'u', 'e'}\n"
     ]
    }
   ],
   "source": [
    "# 返回集合\n",
    "def search4vowels(word):\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found\n",
    "\n",
    "jihe = search4vowels('hello youge')\n",
    "print(jihe)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用注释改进文档\n",
    "* 当我们调用函数时，才知道我们需要输入的参数和返回值的类型“type”\n",
    "* 对此，我们的一种解决办法是把这个信息增加到docstring\n",
    "> python3 注解的记法:\n",
    ">> * 1.函数注解是可选的。\n",
    ">> * 2.函数注解可以提供信息。\n",
    "#### 传递任意数量的实参\n",
    "> * 1.*args，传入多个参数，转化成元组。\n",
    ">> * 假如一个函数定义一个披萨的配料，但并不知道有多少配料需要加入，在参数不确定的情况下，我们引入任意数量的实参。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('paperoni',)\n",
      "('mushroom', 'green papers', 'extra cheese')\n"
     ]
    }
   ],
   "source": [
    "def make_pizza(*args):\n",
    "    toopings = args\n",
    "    print(toopings)\n",
    "    \n",
    "make_pizza(\"paperoni\")\n",
    "make_pizza(\"mushroom\",\"green papers\",\"extra cheese\")\n",
    "#*args的值是一个封装好的空元组，并将所有接收到的值都封装在这个元组里。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> * 2.**kwargs，把关键字参数，转化成字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'youge', 'age': '30', 'job': 'teacher'}\n"
     ]
    }
   ],
   "source": [
    "def infos(**kwargs):\n",
    "    print(kwargs)\n",
    "infos(name=\"youge\",age=\"30\",job=\"teacher\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 导入整个模块\n",
    "> * 1.语法： import  模块名称\n",
    ">> * 模块所在的py文件的名称就是模块的名称\n",
    ">> * 导入模块要注意模块的py文件与主程序文件在一个目录下\n",
    "> * 2.模块导入中的as 用法:\n",
    ">> * 可以用as 自定义一个名称来代替模块的名称\n",
    "### 模块中导入函数\n",
    "> * 1.如果一个模块中有多个函数，但是我只想用其中的一个函数，可以直接导入该函数。\n",
    ">> * 语法 from 模块名称 import 函数名称\n",
    "> * 2.使用星号（*）运算符可以让Python导入模块中的所有函数：\n",
    ">> * 语法 ：  from 模块名 import *\n",
    "###  如何发布一个模块\n",
    "> * 利用 “setuptools” 将模块安装到site-packages 中\n",
    ">> * （1）创建一个发布描述\n",
    ">> * （2）生成一个发布文件\n",
    ">> * （3）安装发布文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.创建setup.py文件\n",
    "\n",
    "#from setuptools import setup\n",
    "#setup(\n",
    "\n",
    "    # name =\"pizza\",\n",
    "    # version =\"1.0\",\n",
    "    # desciption =\"make pizza\",\n",
    "    # author = \"HF Python 2e\",\n",
    "    # author_email=\"374005361@qq.com\",\n",
    "    # url = \"http://example.com/HelloWorld/\",\n",
    "    # py_modules = [\"pizza\"]\n",
    "#)\n",
    "\n",
    "#2.在这个阶段，将setup.py 、  ****.py、README.txt等三个文件发在一个文件夹下\n",
    "\n",
    "#3.在windows上运行，在包含这3个文件的文件夹打开一个命令提示窗口，然后输入以下命令：\n",
    "#py -3 setup.py sdist\n",
    "\n",
    "#4.生成压缩包以后 直接找到该压缩包，使用pip进行安装\n",
    "#py -3 –m pip install ****.zip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数的补充\n",
    "> * 函数可以的引用，函数可以调用函数\n",
    "\n",
    "* 局部变量与全局变量以及其作用域：\n",
    "> * 1.全局变量在整个程序全局都是有效的，局部变量只是在某个函数的内部起作用\n",
    "> * 2.global 在函数内部修改全局变量\n",
    "> * 3.可以使用global 在函数的内部修改全局变量，但是不建议使用，容易造成紊乱\n",
    "\n",
    "* 函数递归\n",
    "> * 递归特性：\n",
    ">> * 1.必须有一个明确的结束条件\n",
    ">> * 2.每次进入更深一层递归时，问题规模相比上次递归应有所减少。\n",
    ">> * 3.递归效率不高，递归次数过多会导致栈溢出（在计算机中，函数调用是通过栈（stack）这种数据结构实现的，每当进入一个函数调用，栈就会多加一层栈帧，每当函数返回，栈就会减少一层栈帧。由于栈的大小不是无限的，所以，递归调用的次数过多，会导致栈溢出。）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数的优点\n",
    "> * 函数的优点之一是将代码与主程序分离；\n",
    "> * 我们可以更进一步，将函数存储在被称为“模块”的独立文件中，再将模块导入主程序；\n",
    "> * import语句允许我们在当前运行的程序文件中使用模块中的代码。\n",
    "\n",
    "* 优势：\n",
    "> * 1.通过将函数存储在独立的文件中，可隐藏程序代码的细节，将重点放在程序的高层逻辑上；\n",
    "> * 2.可以让不同的程序中重用函数；\n",
    "> * 3.可与其他程序员共享这些文件而不是整个程序；\n",
    "> * 4.知道如何导入函数能让你使用其他程序员编写的函数库。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 知识点掌握程度打分\n",
    "> * 模板：\n",
    "```\n",
    "python_base={\n",
    "       \"IDE\":{  \n",
    "               \"IDLE\":[\"操作，100（90）\",\"意义、100（90）\",\n",
    "               \"JUPYTE\":\"\",\n",
    "               \"pycharm\":\"\",\n",
    "                     }\n",
    "                       “XXX”:{}\n",
    "               }\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_base = {\n",
    "    \"列表\":{\n",
    "        \"列表的创建,增加,删除\":{\n",
    "            \"运用\":78,\n",
    "            \"定义理解\":85\n",
    "        },\n",
    "        \"列表的其他方法\":{\n",
    "            \"运用\":80,\n",
    "            \"定义理解\":95\n",
    "        },\n",
    "        \"列表的切片\":{\n",
    "            \"运用\":85,\n",
    "            \"定义\":90\n",
    "        },\n",
    "    },\n",
    "    \"元组\":{\n",
    "        \"元组的创建,取值,定义\":{\n",
    "            \"运用\":90,\n",
    "            \"定义理解\":90\n",
    "        },\n",
    "        \"元组的方法\":{\n",
    "            \"运用\":90,\n",
    "            \"定义理解\":(90)\n",
    "        },\n",
    "    },\n",
    "    \"循环\":{\n",
    "        \"while\":{\n",
    "            \"语法\":85,\n",
    "            \"命令\":90\n",
    "        },\n",
    "        \"for\":{\n",
    "            \"语法\":90,\n",
    "            \"定义\":90\n",
    "        },\n",
    "        \"range\":{\n",
    "            \"语法\":85,\n",
    "            \"定义\":90\n",
    "        },\n",
    "    },\n",
    "    \"字典\":{\n",
    "        \"item\":{\n",
    "            \"用法\":90 # 将key 和 value 组成一个元组，以列表返回\n",
    "        },\n",
    "        \"字典的创建,增加,删除\":{\n",
    "            \"运用\":85,\n",
    "            \"定义理解\":90\n",
    "        },\n",
    "        \"字典的其他方法\":{\n",
    "            \"运用\":80,\n",
    "            \"定义理解\":89\n",
    "        },\n",
    "    },\n",
    "    \"集合\":{\n",
    "        \"集合的创建\":95,\n",
    "        \"定义理解\":95,\n",
    "        \"集合中的方法\":80\n",
    "    },\n",
    "    \"函数\":{\n",
    "        \"运用\":90,\n",
    "        \"定义理解\":95\n",
    "    },\n",
    "    \"模块\":{\n",
    "        \"time\":65,\n",
    "        \"random\":65\n",
    "    },\n",
    "    \"if,elif,else\":{\n",
    "        \"运用\":100,\n",
    "        \"定义理解\":100\n",
    "    }\n",
    "    \n",
    "}\n",
    "\n",
    "python_expand = {\n",
    "    \"zip\":{\n",
    "        \"定义\":100, # \"将对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表\",\n",
    "        \"运用\":80\n",
    "    },\n",
    "    \"拷贝\":[\n",
    "        \"直接赋值\",\n",
    "        \"深拷贝\",\n",
    "        \"浅拷贝\"\n",
    "    ],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "233px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
